(function main(op){
    var d = document;
    //check if the bookmarklet has already been loaded
    //If it's loaded run the applet again will remove the alternation
    if(window.bmGenToc){
        d.body.removeChild(d.getElementById('bcapt'));
        d.body.removeChild(d.getElementById('btab'));
        delete window.bmGenToc;
        return;
    }
    bmGenToc = function(op){
        //create elements used to build toc
        var styleS='<style>.bfixed {position: fixed;color:black;font-size:13px}.bvis{visibility:visible}.bhid{visibility:hidden}.bopen{left:250px}.bclose{left:0px}.bclick{cursor:pointer}#btab{padding:10px;left:0px;top:10%;width:230px;height:80%;background-color:#FFFFCC;overflow:auto}#bcapt{border:0px solid silver;border-right-width:2px;border-bottom-width:2px;top:10%;width:16px;height:60px;background-color:#FFFFCC}</style><!--[if lte IE 6]><style>html,body{margin:0;height:100%;overflow:auto;}.bfixed {position: absolute}</style><![endif]-->';
        var tocS='<div id=\"btab\"class=\"bfixed bvis\"></div><div id=\"bcapt\"class=\"bfixed bopen bclick\">T<br>O<br>C</div>';
        
        var con = d.createElement('div');
        con.innerHTML=(styleS + tocS);   
        while(con.childNodes.length){            
            d.body.appendChild(con.removeChild(con.childNodes[0]));
        }
        
        function getHeaders(){
            var tags = ['h1','h2','h3','h4','h5'];    
            //get all headers
            function helper(par,tags,list){
                var h = tags.shift();
                var heads = par.getElementsByTagName(h);
                var n = heads.length;
                var i;
                if(n === 0 && tags.length>0){
                    helper(par,tags.slice(0),list);
                }else{
                    for(i = 0; i !== n; i++){
                        list.push(heads[i]);
                        if(tags.length>0){
                            helper(heads[i].parentNode,tags.slice(0),list);
                        }
                    }
                }
                return list;
            }
            
            return helper(d,tags,[]);
        }
        
        function indent(n){
            var ind = '';
            for(var i = 1; i< n;i++){ind+='&nbsp;';}
            return ind;
        }
        
        //var nav, con, border;
        
        //if the caption is clicked the tab will be collapsed or expanded accordingly
        function toggleCls(el,a,b){
            el.className=el.className.replace(a,b);
        }
        
        function toggleCapt(){     
            var t= d.getElementById('btab');
            var c = d.getElementById('bcapt');
            if(c.className.indexOf('bopen')>=0){
                    toggleCls(t,'bvis','bhid');
                    toggleCls(c,'bopen','bclose');
            }else{
                    toggleCls(t,'bhid','bvis');
                    toggleCls(c,'bclose','bopen');
            }
            //console.log(t.className);
        }
        d.getElementById('bcapt').onclick=toggleCapt;

        
        //generate toc list, which resides inside btab
        function genNavBar(){
            
            function onLinkClick(e){
                    this.toH.scrollIntoView(true);
                    //this.getUserData('header').scrollIntoView(true);                
            }
            var heads = getHeaders();
            if(heads.length){
               
                               
                var tab = d.getElementById('btab');
                //generate toc and put it into navbar                       
                for(var i = 0; i!= heads.length;i++){
                    var h = heads[i];
                    var l = d.createElement('p');
                    //l.setUserData('header',h,null);
                    l.toH=h;
                    l.innerHTML = indent(parseInt(h.tagName.substring(1),10))+h.textContent;
                    l.className='bclick';
                    l.onclick = onLinkClick;
                    tab.appendChild(l);
                }
                
            }
            else{
                delete window.bmGenToc;
            }
            
        }
        genNavBar();
    };
    bmGenToc();
})();
